﻿using System;
using Orchard.DisplayManagement;
using Orchard.Events;
using Orchard.Localization;
using Orchard.Logging;

namespace Whatsnexx.Rules
{
    using System.Linq;
    using System.Web.Mvc;
    using Orchard.ContentManagement.MetaData;

    public interface IFormProvider : IEventHandler
    {
        void Describe(dynamic context);
    }

    public class TicketBusForms : IFormProvider
    {
        private readonly IContentDefinitionManager _contentDefinitionManager;
        protected dynamic Shape { get; set; }
        public Localizer T { get; set; }
        public ILogger Logger { get; set; }

        public TicketBusForms(IShapeFactory shapeFactory,
            IContentDefinitionManager contentDefinitionManager)
        {
            _contentDefinitionManager = contentDefinitionManager;
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
            Logger = NullLogger.Instance;
        }

        public void Describe(dynamic context)
        {

            Func<IShapeFactory, dynamic> form =
                shape =>
                    {
                        var f = Shape.Form(
                            Id: "ActionSendEvent",
                            _Parts: Shape.SelectList(
                                Id: "contenttypes", Name: "ContentTypes",
                                Title: T("Content types"),
                                Description: T("Select some content types."),
                                Size: 10,
                                Multiple: true
                                )
                            );

                        f._Parts.Add(new SelectListItem { Value = "", Text = T("Any").Text });

                        foreach (var contentType in _contentDefinitionManager.ListTypeDefinitions().OrderBy(x => x.DisplayName))
                        {
                            f._Parts.Add(new SelectListItem { Value = contentType.Name, Text = contentType.DisplayName });
                        }

                        return f;
                    };
            context.Form("ActionSendEvent", form);
        }
    }

    public interface IFormHandler : IEventHandler
    {
        void Validating(dynamic context);
    }

    public class PublishingFormsValitator : IFormHandler
    {
        public PublishingFormsValitator()
        {
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public void Validating(dynamic context)
        {
            if (context.FormName != "ActionSendEvent")
                return;
           
        }
    }
}